/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


$(function () {
    var name2Id = {};//姓名对应的id
    $.fn.typeahead.Constructor.prototype.blur = function () {
        var that = this;
        setTimeout(function () {
            that.hide()
        }, 250);
    };
    $("#UserName").typeahead({
        source: function (query, process) {
//return ["红蒜小混级","红蒜大混级","一般红蒜","白蒜小混级","白蒜大混级","一般白蒜"];
            $.post('getUserByName', {text: query}, function (data) {
                var results = $.map(data, function (item) {
                    name2Id[item.user_nickname] = item.id;//键值对保存下来。
                    return item.user_nickname;
                });
                process(results);
            }, 'json');
        },
        updater: function (id) {
//            var results = $.map(dataresult, function (item) {
//                return item.cropname;
//            });
            console.log("updater:" + name2Id[id]);
            $("#UserId").val(name2Id[id]);
            setLoan(name2Id[id]);
            return id;
        }, afterSelect: function (item) {
            console.log("afterSelect:" + name2Id[id]);//打印对应的id
            // $("#UserId").val(name2Id[id]);
        },

    });

    $('#LoanId').change(function () {
        var id = $(this).children('option:selected').val();//这就是selected的值 
        $.post('getLoanById', {id: id}, function (data) {
            $("#ApplyAmount").val(data.ApplyAmount);
            $("#RepayMentDateTime").val(data.RepayMentDateTime);
            $("#SeedAmount").val(data.SeedAmount);
        }, 'json');
    })
});
function setLoan(userid) {
    $.post('getLoanByDriver', {userid: userid}, function (data) {
        $("#LoanId").html(data);
        var id = $("#LoanId").children('option:selected').val();//这就是selected的值 
        $.post('getLoanById', {id: id}, function (data) {
            $("#ApplyAmount").val(data.ApplyAmount);
            $("#RepayMentDateTime").val(data.RepayMentDateTime);
            $("#SeedAmount").val(data.SeedAmount);
        }, 'json');
    });
}